﻿using CefSharp;
using Chen.CefWebKit.Handler;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Chen.CefWebKit.WebBrowser
{
    public partial class BrowserForm : Form
    {
        
        public BrowserForm()
        {
            InitializeComponent();

            if (!Cef.IsInitialized)
            {
                var setting = new CefSharp.CefSettings();
                setting.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
                setting.LogSeverity = LogSeverity.Disable;
                Cef.Initialize(setting);
            }
            new System.Threading.Thread((System.Threading.ThreadStart)delegate
            {
                try
                {
                    Application.Run(this);
                }
                catch
                {

                }
            }).Start();
        }

        private void BrowserForm_Load(object sender, EventArgs e)
        {
            browserTabControl.Dock = DockStyle.Fill;
        }
        public void AddTask(string TaskName,BrowserTabUserControl browser)
        {
            this.Invoke(new Action(() => {
                browserTabControl.SuspendLayout();

                var tabPage = new TabPage(TaskName)
                {
                    Dock = DockStyle.Fill
                };
                browser.CreateControl();
                tabPage.Controls.Add(browser);
                browserTabControl.TabPages.Add(tabPage);
                browserTabControl.SelectedTab = tabPage;
                browserTabControl.ResumeLayout(true);
            }));

        }
    }
}
